const proxy = require('http-proxy-middleware')

module.exports = function (app) {
  app.use(
    // https://cnodejs.org/api/v1/topics
    // curl http://localhost:3000/api/v1/topics
    proxy(
      '/api/v1', {
        target: 'https://cnodejs.org', // target host
        changeOrigin: true,               // needed for virtual hosted sites
        ws: false,                         // proxy websockets
        pathRewrite: {
          '^/api/old-path' : '/api/new-path',     // rewrite path
          '^/api/remove/path' : '/path'           // remove base path
        },
        router: {
          // when request.headers.host == 'dev.localhost:3000',
          // override target 'http://www.example.org' to 'http://localhost:8000'
          'dev.localhost:3000' : 'http://localhost:8000'
        }
      }
    ),
    // https://cnodejs.org/api/v1/topic/5c2d6df876c4964062a1ffe2
    // curl http://localhost:3000/api/aaa/topic/5c2d6df876c4964062a1ffe2
    proxy(
      '/api/aaa', {
        target: 'https://cnodejs.org',
        changeOrigin: true,
        pathRewrite: {
          '^/api/aaa' : '/api/v1',     // rewrite path
          '^/api/remove/path' : '/path'           // remove base path
        },
      }
    ),
  )
}